﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using Fleck;
using System.Runtime.Serialization.Json;
using System.IO;
namespace GameServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //FleckLog.Level = LogLevel.Debug;
            var allSockets = new List<IWebSocketConnection>();
            WebSocketServer server = new WebSocketServer("ws://0.0.0.0:8181");
            GameControl game = new GameControl(allSockets);
            GamePlayer replay = new GamePlayer();
            List<String> AI_filenames = new List<String>();
            FrmMain frmMain = new FrmMain(ref AI_filenames);
            Thread batch_battle = null;
            //启动WebSocket 服务器
            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    Console.WriteLine("Open!");
                    allSockets.Add(socket);
                };
                socket.OnClose = () =>
                {
                    Console.WriteLine("Close!");
                    allSockets.Remove(socket);
                };
                socket.OnMessage = message =>
                {
                    Console.WriteLine(message);
                    if (message == "Start")
                    {
                        if (game != null)
                            game.Stop();
                        if (replay != null)
                            replay.Stop();
                        if (batch_battle != null)
                            batch_battle.Abort();
                        switch (frmMain.server_type)
                        {
                            case "Battle":
                                game = new GameControl(allSockets, AI_filenames);
                                game.Start();
                                game.Continue();
                                Console.WriteLine("Game Started!");
                                break;
                            case "Replay":
                                replay = new GamePlayer(allSockets, frmMain.replay_path);
                                replay.Start();
                                replay.Continue();
                                Console.WriteLine("RePlay Started!");
                                break;
                            case "Battle_by_file":
                                batch_battle = new Thread(() =>
                                {
                                    StreamReader sr = new StreamReader(frmMain.battle_file_path,Encoding.Default);
                                    while (!sr.EndOfStream)
                                    {
                                        AI_filenames.Clear();
                                        String[] ais = sr.ReadLine().Split(' ');
                                        if (ais.Length != 4)
                                            continue;
                                        foreach (String ai in ais)
                                            AI_filenames.Add(ai);
                                        game = new GameControl(allSockets, AI_filenames);
                                        AutoResetEvent game_running = new AutoResetEvent(false);
                                        game.Start(game_running);
                                        game.Continue();
                                        game_running.WaitOne();
                                    }
                                });
                                batch_battle.Start();
                                break;

                        }
                    }
                };
            });
            //[STAThreadAttribute]
            Thread frmMain_thread = new Thread(() =>
            {
                System.Windows.Forms.Application.Run(frmMain);
            });
            frmMain_thread.TrySetApartmentState(ApartmentState.STA);
            frmMain_thread.Start();
            //frmMain.ShowDialog();
            //var input = Console.ReadLine();
            //while (input != "exit")
            //{
            //    input = Console.ReadLine();
            //}
        }
    }
}
